Array.prototype.EACH_END = "$$$^&*!$$$";
Array.prototype.each = function(fn , scope) {
    var len = this.length;
    for(var i = 0 ; i < len ; i++) {
        var result = fn.call(scope || this, this[i] , i);
        if(result === this.EACH_END) {
            break;
        }
    }
};
Array.prototype.contains = function(object , compareFun) {
    if(this.indexOf(object) != -1) {
        return true;
    }
    var find = false, array = this;
    if(compareFun) {
        this.each(function(obj) {
            if(compareFun.call(this , object , obj)) {
                find = true;
                return array.EACH_END;
            }
        });
    }
    return find;
};
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
};
String.prototype.contains = function(str) {
    return this.indexOf(str) != -1;
};
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
};
String.prototype.filterNull = function() {
    if(this == 'null') {
        return null;
    }
    return this;
};
String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
};
String.prototype.endsWith = function (str){
    return this.slice(-str.length) == str;
};
Date.prototype.format = function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    };

    if(/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
};
var BeanUtils = {
    copyProperties : function(srcObject , dstObject , override) {
        if(override === undefined) {
            override = true;//如果没有传递数据，则以JS为准
        }
        for(var p in srcObject) {
            if(override || (!dstObject[p])) {
                dstObject[p] = srcObject[p];
            }
        }
    }
};
var Map = function() {
    var object = {};
    var size = 0;
    var prefix = "k_";
    var simpleObject = "$$$";
    this.put = function(key , value) {
        object[prefix + key] = value;
        size++;
    };
    this.add = function(key) {
        object[prefix + key] = null;
        size++;
    };
    this.get = function(key) {
        var v = object[prefix + key];
        return v == simpleObject ? null : v;
    };
    this.contains = function(key) {
        return this.get(key) !== undefined;
    };
    this.remove = function(key) {
        if(this.contains(key)) {
            size--;
            delete object[prefix + key];
        }
    };
    this.clear = function() {
        object = {};
        size = 0;
    };
    this.getKeys = function() {
        var array = [];
        for(var p in object) {
            array.push(p.substring(prefix.length));
        }
        array.sort();
        return array;
    };
    this.size = function() {
        return size;
    };
};
var List = function() {
    var array = [];
    this.add = function(object) {
        array.put(object);
    };
    this.get = function(index) {
        return array[i];
    };
    this.remove = function(index) {
        array.splice(index , 1);
    };
    this.contains = function(object ,compareFun) {
        return array.contains(object , compareFun);
    };
    this.clear = function() {
        array = [];
    };
    this.size = function() {
        return array.length;
    };
};
var comm_tools = {
    jsonToUrl : function(json) {
        var data = '';
        for(var p in json) {
            data += p + "=" + encodeURIComponent(json[p] || '') + "&";
        }
        return data;
    },

    getSizeName:function(size , fixed) {
        size = size*1;
        if(size) {
            fixed = fixed || 0;
            if(size < 1024) {
                return size.toFixed(fixed) + "B";
            }
            if(size < 1024 * 1024) {
                return (parseFloat(size) / 1024).toFixed(fixed) + "KB";
            }
            if(size < 1024 * 1024 * 1024) {
                return (parseFloat(size) / 1024 / 1024).toFixed(fixed) + "MB";
            }
            if(size < 1024 * 1024 * 1024 * 1024) {
                return (parseFloat(size) / 1024 / 1024 / 1024).toFixed(fixed) + "GB";
            }
            return (parseFloat(size) / 1024 / 1024 / 1024 / 1024).toFixed(fixed) + "TB";
        }else {
            return "0B";
        }
    },
    compareFormattedSize:function(size1,size2) {//size1>=size2 :true size1<size2 : false
        if(size1 == "--" || size2 == "--")
            return false;
        var ulen1 = (size1[size1.length-4] >= '0' &&size1[size1.length-4]<='9') ? 3 :4 ;
        var ulen2 = (size2[size2.length-4] >= '0' &&size2[size2.length-4]<='9') ? 3 :4 ;//计算单位的长度
        var unit1 = size1.substring(size1.length-ulen1);
        var unit2 = size2.substring(size2.length-ulen2);
        var standard = ["B/s","KB/s","MB/s","GB/s","TB/s"];
        if(unit1 != unit2) {
            for(var p = 0 ;p < standard.length; p++) {
                if(standard[p] == unit1)//先遇到unit1说明unit1<unit2
                    return false;
                if(standard[p] == unit2)
                    return true;
            }
        }else {//单位相同，比较数字
            var value1 = size1.substring(0, size1.length - ulen1);
            var value2 = size2.substring(0, size2.length - ulen2);
            if(parseInt(value1,10)>=parseInt(value2,10))
                return true;
            else
                return false;
        }
    },
    numberWithCommas: function (num) {//将整数1234567转换为1,234,567
        return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
};

var ajax = function(prop) {
    if(!prop.data && prop.jsonData) {
        var json = prop.jsonData;
        prop.data = comm_tools.jsonToUrl(json);
    }
    if(!prop.data) {
        prop.data = "";
    }
    var callBackSuccess = function(data) {
        if(prop.success) {
            prop.success.call(prop.scope || this , data);
        }
    };
    var callBackError = function(data) {
        if(prop.error) {
            prop.error.call(prop.scope || this , data);
        }
    };
    $.ajax({
        method : prop.method || 'POST',//默认
        contentType:prop.contentType || "application/x-www-form-urlencoded;charset=UTF-8",
        data:prop.data || '',
        url:prop.url,
        dataType:'json',
        success:function(json) {
            if(prop.callBack) {
                prop.callBack.call(this , json);
            }
            if(!json.success) {
                if(json.code == sys_config.NOT_LOGIN) {
                    alert('出错' , "用户未进行登陆。" , 'error');
                }else if(json.code == sys_config.NOT_LOGIN_OS) {
                    alert('出错' , "用户未进行登陆任何操作系统。" , 'error');
                }else {
                    callBackSuccess(json);
                }
            }else {
                callBackSuccess(json);
            }
        },
        error:function(json) {
            if(prop.callBack) {
                prop.callBack.call(this , json);
            }
            callBackError(json);
        }
    });
};
var sys_config = {
    USER_COOKIE_KEY : "__DMS_USER_LOGIN_COOKIE_KEY__",
    NOT_LOGIN : "NOT_LOGIN",

    DATE_TIME_FORMAT : "yyyy-MM-dd hh:mm:ss",
    DATE_FORMAT : "yyyy-MM-dd",
    TIME_FORMAT:"hh:mm:ss"
};